# set working directory #
library(psych)

## MERGE THE SEPARATE DAILY DIARIES INTO ONE DATAFRAME ####

# read each daily diary csv file as a separate dataframe #
daily1 <- read.csv("daily1.csv", header=TRUE, stringsAsFactors = FALSE)
daily2 <- read.csv("daily2.csv", header=TRUE, stringsAsFactors = FALSE)
daily3 <- read.csv("daily3.csv", header=TRUE, stringsAsFactors = FALSE)
daily4 <- read.csv("daily4.csv", header=TRUE, stringsAsFactors = FALSE)
daily5 <- read.csv("daily5.csv", header=TRUE, stringsAsFactors = FALSE)
daily6 <- read.csv("daily6.csv", header=TRUE, stringsAsFactors = FALSE)
daily7 <- read.csv("daily7.csv", header=TRUE, stringsAsFactors = FALSE)
makeup1 <- read.csv("makeup1.csv", header=TRUE, stringsAsFactors = FALSE)
makeup2 <- read.csv("makeup2.csv", header=TRUE, stringsAsFactors = FALSE)
makeup3 <- read.csv("makeup3.csv", header=TRUE, stringsAsFactors = FALSE)

# for merging data differently -- see starting line 181 #
daily1.copy <- daily1; daily2.copy <- daily2; daily3.copy <- daily3; daily4.copy <- daily4
daily5.copy <- daily5; daily6.copy <- daily6; daily7.copy <- daily7; 
makeup1.copy <- makeup1; makeup2.copy <- makeup2; makeup3.copy <- makeup3

# rename the columns of different files for merging #
colnames(daily1) <- paste(colnames(daily1), "1", sep = "_")
colnames(daily2) <- paste(colnames(daily2), "2", sep = "_")
colnames(daily3) <- paste(colnames(daily3), "3", sep = "_")
colnames(daily4) <- paste(colnames(daily4), "4", sep = "_")
colnames(daily5) <- paste(colnames(daily5), "5", sep = "_")
colnames(daily6) <- paste(colnames(daily6), "6", sep = "_")
colnames(daily7) <- paste(colnames(daily7), "7", sep = "_")
colnames(makeup1) <- paste(colnames(makeup1), "8", sep = "_")
colnames(makeup2) <- paste(colnames(makeup2), "9", sep = "_")
colnames(makeup3) <- paste(colnames(makeup3), "0", sep = "_")

# merge the files #
merged1<- merge(daily1, daily2, by.x="anonymousId_1", by.y="anonymousId_2", all=TRUE)
merged2<- merge(merged1, daily3, by.x="anonymousId_1", by.y="anonymousId_3", all=TRUE)
merged3<- merge(merged2, daily4, by.x="anonymousId_1", by.y="anonymousId_4", all=TRUE)
merged4<- merge(merged3, daily5, by.x="anonymousId_1", by.y="anonymousId_5", all=TRUE)
merged5<- merge(merged4, daily6, by.x="anonymousId_1", by.y="anonymousId_6", all=TRUE)
merged6<- merge(merged5, daily7, by.x="anonymousId_1", by.y="anonymousId_7", all=TRUE)
merged7<- merge(merged6, makeup1, by.x="anonymousId_1", by.y="anonymousId_8", all=TRUE)
merged8<- merge(merged7, makeup2, by.x="anonymousId_1", by.y="anonymousId_9", all=TRUE)
merged.data<- merge(merged8, makeup3, by.x="anonymousId_1", by.y="anonymousId_0", all=TRUE)

# count how many of the daily diaries the mturkers did #
merged.data$daily1attempted <- NA; merged.data$daily1attempted[merged.data$panas_30_1>0] <- 1;
merged.data$daily2attempted <- NA; merged.data$daily2attempted[merged.data$panas_30_2>0] <- 1;
merged.data$daily3attempted <- NA; merged.data$daily3attempted[merged.data$panas_30_3>0] <- 1;
merged.data$daily4attempted <- NA; merged.data$daily4attempted[merged.data$panas_30_4>0] <- 1;
merged.data$daily5attempted <- NA; merged.data$daily5attempted[merged.data$panas_30_5>0] <- 1;
merged.data$daily6attempted <- NA; merged.data$daily6attempted[merged.data$panas_30_6>0] <- 1;
merged.data$daily7attempted <- NA; merged.data$daily7attempted[merged.data$panas_30_7>0] <- 1;
merged.data$makeup1attempted <- NA; merged.data$makeup1attempted[merged.data$panas_30_8>0] <- 1;
merged.data$makeup2attempted <- NA; merged.data$makeup2attempted[merged.data$panas_30_9>0] <- 1;
merged.data$makeup3attempted <- NA; merged.data$makeup3attempted[merged.data$panas_30_0>0] <- 1;

# calculate total daily diaries completed #
merged.data$dailytotal<- with(merged.data, (rowSums(cbind(daily1attempted,daily2attempted,daily3attempted,daily4attempted,daily5attempted,daily6attempted,daily7attempted, makeup1attempted, makeup2attempted, makeup3attempted), na.rm=TRUE)))

# see daily diary total as a table #
table(merged.data$dailytotal)

## CALCULATE THE ADJUSTMENT VARIABLES ####

## life satisfaction ##

# calculate the total swls per day for each participant #
merged.data$day1swls <- with(merged.data, (rowMeans(cbind(slws_4_1, slws_5_1, slws_6_1, slws_7_1, slws_8_1), na.rm=TRUE)))
merged.data$day2swls <- with(merged.data, (rowMeans(cbind(slws_4_2, slws_5_2, slws_6_2, slws_7_2, slws_8_2), na.rm=TRUE)))
merged.data$day3swls <- with(merged.data, (rowMeans(cbind(slws_4_3, slws_5_3, slws_6_3, slws_7_3, slws_8_3), na.rm=TRUE)))
merged.data$day4swls <- with(merged.data, (rowMeans(cbind(slws_4_4, slws_5_4, slws_6_4, slws_7_4, slws_8_4), na.rm=TRUE)))
merged.data$day5swls <- with(merged.data, (rowMeans(cbind(slws_4_5, slws_5_5, slws_6_5, slws_7_5, slws_8_5), na.rm=TRUE)))
merged.data$day6swls <- with(merged.data, (rowMeans(cbind(slws_4_6, slws_5_6, slws_6_6, slws_7_6, slws_8_6), na.rm=TRUE)))
merged.data$day7swls <- with(merged.data, (rowMeans(cbind(slws_4_7, slws_5_7, slws_6_7, slws_7_7, slws_8_7), na.rm=TRUE)))
merged.data$makeup1swls <- with(merged.data, (rowMeans(cbind(slws_4_8, slws_5_8, slws_6_8, slws_7_8, slws_8_8), na.rm=TRUE)))
merged.data$makeup2swls <- with(merged.data, (rowMeans(cbind(slws_4_9, slws_5_9, slws_6_9, slws_7_9, slws_8_9), na.rm=TRUE)))
merged.data$makeup3swls <- with(merged.data, (rowMeans(cbind(slws_4_0, slws_5_0, slws_6_0, slws_7_0, slws_8_0), na.rm=TRUE)))

# descriptives of slws each day #
with(merged.data, (describe(cbind(day1swls, day2swls, day3swls, day4swls, day5swls, day6swls, day7swls, makeup1swls, makeup2swls, makeup3swls))))

# calculate reliability of swls scale (for every day of the study) #
# in the report we averaged the reliability for the 10 days (i.e., the 7 days plus the 3 make-up days) 
with(merged.data, alpha(data.frame(slws_4_1, slws_5_1, slws_6_1, slws_7_1, slws_8_1), na.rm=TRUE))
with(merged.data, alpha(data.frame(slws_4_2, slws_5_2, slws_6_2, slws_7_2, slws_8_2), na.rm=TRUE))
with(merged.data, alpha(data.frame(slws_4_3, slws_5_3, slws_6_3, slws_7_3, slws_8_3), na.rm=TRUE))
with(merged.data, alpha(data.frame(slws_4_4, slws_5_4, slws_6_4, slws_7_4, slws_8_4), na.rm=TRUE))
with(merged.data, alpha(data.frame(slws_4_5, slws_5_5, slws_6_5, slws_7_5, slws_8_5), na.rm=TRUE))
with(merged.data, alpha(data.frame(slws_4_6, slws_5_6, slws_6_6, slws_7_6, slws_8_6), na.rm=TRUE))
with(merged.data, alpha(data.frame(slws_4_7, slws_5_7, slws_6_6, slws_7_7, slws_8_7), na.rm=TRUE))
with(merged.data, alpha(data.frame(slws_4_8, slws_5_8, slws_6_8, slws_7_8, slws_8_8), na.rm=TRUE))
with(merged.data, alpha(data.frame(slws_4_9, slws_5_9, slws_6_9, slws_7_9, slws_8_9), na.rm=TRUE))
with(merged.data, alpha(data.frame(slws_4_0, slws_5_0, slws_6_0, slws_7_0, slws_8_0), na.rm=TRUE))

# calculate the average swls per rater across all days #
merged.data$averageswls <- with(merged.data, (rowMeans(cbind(day1swls, day2swls, day3swls, day4swls, day5swls, day6swls, day7swls, makeup1swls, makeup2swls, makeup3swls), na.rm=TRUE)))

# descriptives of average swls across all days #
describe(merged.data$averageswls)

# calculate reliability of swls across days per rater (ignoring the make up days because there's too much missing data #
with(merged.data, alpha(data.frame(day1swls, day2swls, day3swls, day4swls, day5swls, day6swls, day7swls), na.rm=TRUE))

## positive and negative affectivity (PANAS) ####

# note that the variable names were labeled weirdly #
# PA = (panas_4_1, panas_6_1, panas_8_1, panas_19_1, panas_20_1, panas_22_1, panas_24_1, panas_26_1, panas_27_1, panas_29_1)
# NA = panas_5_1, panas_7_1, panas_16_1, panas_17_1, panas_18_1, panas_21_1, panas_23_1, panas_25_1, panas_28_1, panas_30_1)

# calcualate positive emotion per day #
merged.data$day1PA <- with(merged.data, (rowMeans(cbind(panas_4_1, panas_6_1, panas_8_1, panas_19_1, panas_20_1, panas_22_1, panas_24_1, panas_26_1, panas_27_1, panas_29_1), na.rm=TRUE)))
merged.data$day2PA <- with(merged.data, (rowMeans(cbind(panas_4_2, panas_6_2, panas_8_2, panas_19_2, panas_20_2, panas_22_2, panas_24_2, panas_26_2, panas_27_2, panas_29_2), na.rm=TRUE)))
merged.data$day3PA <- with(merged.data, (rowMeans(cbind(panas_4_3, panas_6_3, panas_8_3, panas_19_3, panas_20_3, panas_22_3, panas_24_3, panas_26_3, panas_27_3, panas_29_3), na.rm=TRUE)))
merged.data$day4PA <- with(merged.data, (rowMeans(cbind(panas_4_4, panas_6_4, panas_8_4, panas_19_4, panas_20_4, panas_22_4, panas_24_4, panas_26_4, panas_27_4, panas_29_4), na.rm=TRUE)))
merged.data$day5PA <- with(merged.data, (rowMeans(cbind(panas_4_5, panas_6_5, panas_8_5, panas_19_5, panas_20_5, panas_22_5, panas_24_5, panas_26_5, panas_27_5, panas_29_5), na.rm=TRUE)))
merged.data$day6PA <- with(merged.data, (rowMeans(cbind(panas_4_6, panas_6_6, panas_8_6, panas_19_6, panas_20_6, panas_22_6, panas_24_6, panas_26_6, panas_27_6, panas_29_6), na.rm=TRUE)))
merged.data$day7PA <- with(merged.data, (rowMeans(cbind(panas_4_7, panas_6_7, panas_8_7, panas_19_7, panas_20_7, panas_22_7, panas_24_7, panas_26_7, panas_27_7, panas_29_7), na.rm=TRUE)))
merged.data$makeup1PA <- with(merged.data, (rowMeans(cbind(panas_4_8, panas_6_8, panas_8_8, panas_19_8, panas_20_8, panas_22_8, panas_24_8, panas_26_8, panas_27_8, panas_29_8), na.rm=TRUE)))
merged.data$makeup2PA <- with(merged.data, (rowMeans(cbind(panas_4_9, panas_6_9, panas_8_9, panas_19_9, panas_20_9, panas_22_9, panas_24_9, panas_26_9, panas_27_9, panas_29_9), na.rm=TRUE)))
merged.data$makeup3PA <- with(merged.data, (rowMeans(cbind(panas_4_0, panas_6_0, panas_8_0, panas_19_0, panas_20_0, panas_22_0, panas_24_0, panas_26_0, panas_27_0, panas_29_0), na.rm=TRUE)))

# calculate reliability of PA scale #
with(merged.data, (alpha(data.frame(panas_4_1, panas_6_1, panas_8_1, panas_19_1, panas_20_1, panas_22_1, panas_24_1, panas_26_1, panas_27_1, panas_29_1), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_4_2, panas_6_2, panas_8_2, panas_19_2, panas_20_2, panas_22_2, panas_24_2, panas_26_2, panas_27_2, panas_29_2), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_4_3, panas_6_3, panas_8_3, panas_19_3, panas_20_3, panas_22_3, panas_24_3, panas_26_3, panas_27_3, panas_29_3), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_4_4, panas_6_4, panas_8_4, panas_19_4, panas_20_4, panas_22_4, panas_24_4, panas_26_4, panas_27_4, panas_29_4), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_4_5, panas_6_5, panas_8_5, panas_19_5, panas_20_5, panas_22_5, panas_24_5, panas_26_5, panas_27_5, panas_29_5), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_4_6, panas_6_6, panas_8_6, panas_19_6, panas_20_6, panas_22_6, panas_24_6, panas_26_6, panas_27_6, panas_29_6), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_4_7, panas_6_7, panas_8_7, panas_19_7, panas_20_7, panas_22_7, panas_24_7, panas_26_7, panas_27_7, panas_29_7), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_4_8, panas_6_8, panas_8_8, panas_19_8, panas_20_8, panas_22_8, panas_24_8, panas_26_8, panas_27_8, panas_29_8), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_4_9, panas_6_9, panas_8_9, panas_19_9, panas_20_9, panas_22_9, panas_24_9, panas_26_9, panas_27_9, panas_29_9), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_4_0, panas_6_0, panas_8_0, panas_19_0, panas_20_0, panas_22_0, panas_24_0, panas_26_0, panas_27_0, panas_29_0), na.rm=TRUE)))

# calculate negative emotion per day #
merged.data$day1NA <- with(merged.data, (rowMeans(cbind(panas_5_1, panas_7_1, panas_16_1, panas_17_1, panas_18_1, panas_21_1, panas_23_1, panas_25_1, panas_28_1, panas_30_1), na.rm=TRUE)))
merged.data$day2NA <- with(merged.data, (rowMeans(cbind(panas_5_2, panas_7_2, panas_16_2, panas_17_2, panas_18_2, panas_21_2, panas_23_2, panas_25_2, panas_28_2, panas_30_2), na.rm=TRUE)))
merged.data$day3NA <- with(merged.data, (rowMeans(cbind(panas_5_3, panas_7_3, panas_16_3, panas_17_3, panas_18_3, panas_21_3, panas_23_3, panas_25_3, panas_28_3, panas_30_3), na.rm=TRUE)))
merged.data$day4NA <- with(merged.data, (rowMeans(cbind(panas_5_4, panas_7_4, panas_16_4, panas_17_4, panas_18_4, panas_21_4, panas_23_4, panas_25_4, panas_28_4, panas_30_4), na.rm=TRUE)))
merged.data$day5NA <- with(merged.data, (rowMeans(cbind(panas_5_5, panas_7_5, panas_16_5, panas_17_5, panas_18_5, panas_21_5, panas_23_5, panas_25_5, panas_28_5, panas_30_5), na.rm=TRUE)))
merged.data$day6NA <- with(merged.data, (rowMeans(cbind(panas_5_6, panas_7_6, panas_16_6, panas_17_6, panas_18_6, panas_21_6, panas_23_6, panas_25_6, panas_28_6, panas_30_6), na.rm=TRUE)))
merged.data$day7NA <- with(merged.data, (rowMeans(cbind(panas_5_7, panas_7_7, panas_16_7, panas_17_7, panas_18_7, panas_21_7, panas_23_7, panas_25_7, panas_28_7, panas_30_7), na.rm=TRUE)))
merged.data$makeup1NA <- with(merged.data, (rowMeans(cbind(panas_5_8, panas_7_8, panas_16_8, panas_17_8, panas_18_8, panas_21_8, panas_23_8, panas_25_8, panas_28_8, panas_30_8), na.rm=TRUE)))
merged.data$makeup2NA <- with(merged.data, (rowMeans(cbind(panas_5_9, panas_7_9, panas_16_9, panas_17_9, panas_18_9, panas_21_9, panas_23_9, panas_25_9, panas_28_9, panas_30_9), na.rm=TRUE)))
merged.data$makeup3NA <- with(merged.data, (rowMeans(cbind(panas_5_0, panas_7_0, panas_16_0, panas_17_0, panas_18_0, panas_21_0, panas_23_0, panas_25_0, panas_28_0, panas_30_0), na.rm=TRUE)))

# calculate reliability of NA scale #
with(merged.data, (alpha(data.frame(panas_5_1, panas_7_1, panas_16_1, panas_17_1, panas_18_1, panas_21_1, panas_23_1, panas_25_1, panas_28_1, panas_30_1), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_5_2, panas_7_2, panas_16_2, panas_17_2, panas_18_2, panas_21_2, panas_23_2, panas_25_2, panas_28_2, panas_30_2), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_5_3, panas_7_3, panas_16_3, panas_17_3, panas_18_3, panas_21_3, panas_23_3, panas_25_3, panas_28_3, panas_30_3), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_5_4, panas_7_4, panas_16_4, panas_17_4, panas_18_4, panas_21_4, panas_23_4, panas_25_4, panas_28_4, panas_30_4), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_5_5, panas_7_5, panas_16_5, panas_17_5, panas_18_5, panas_21_5, panas_23_5, panas_25_5, panas_28_5, panas_30_5), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_5_6, panas_7_6, panas_16_6, panas_17_6, panas_18_6, panas_21_6, panas_23_6, panas_25_6, panas_28_6, panas_30_6), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_5_7, panas_7_7, panas_16_7, panas_17_7, panas_18_7, panas_21_7, panas_23_7, panas_25_7, panas_28_7, panas_30_7), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_5_8, panas_7_8, panas_16_8, panas_17_8, panas_18_8, panas_21_8, panas_23_8, panas_25_8, panas_28_8, panas_30_8), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_5_9, panas_7_9, panas_16_9, panas_17_9, panas_18_9, panas_21_9, panas_23_9, panas_25_9, panas_28_9, panas_30_9), na.rm=TRUE)))
with(merged.data, (alpha(data.frame(panas_5_0, panas_7_0, panas_16_0, panas_17_0, panas_18_0, panas_21_0, panas_23_0, panas_25_0, panas_28_0, panas_30_0), na.rm=TRUE)))

# calculate average PA per rater across all days #
merged.data$averagePA <- with(merged.data, (rowMeans(cbind(day1PA, day2PA, day3PA, day4PA, day5PA, day6PA, day7PA, makeup1PA, makeup2PA, makeup3PA), na.rm=TRUE)))

# calculate average NA per rater across all days #
merged.data$averageNA <- with(merged.data, (rowMeans(cbind(day1NA, day2NA, day3NA, day4NA, day5NA, day6NA, day7NA, makeup1NA, makeup2NA, makeup3NA), na.rm=TRUE)))

with(merged.data, describe(cbind(averagePA,averageNA)))

# calculate reliability of PA across days per rater #
with(merged.data, (alpha(data.frame(day1PA, day2PA, day3PA, day4PA, day5PA, day6PA, day7PA, makeup1PA, makeup2PA), na.rm=TRUE)))

# calculate reliability of NA across days per rater #
with(merged.data, (alpha(data.frame(day1NA, day2NA, day3NA, day4NA, day5NA, day6NA, day7NA, makeup1NA), na.rm=TRUE)))

# correlations of swls, NA, PA ####
with(merged.data, describe(cbind(averageswls, averageNA, averagePA)))
with(merged.data, corr.test(cbind(averageswls, averageNA, averagePA)))

# calculate response to honest answering #
describe(merged.data$Honesty)
table(merged.data$Honesty)

# merge data differently so that each diary completed by anyone is a row ####
daily1.copy$diary.number <- 1; daily1.copy$Honesty <- NA
daily2.copy$diary.number <- 2; daily2.copy$consent <- NA; daily2.copy$Honesty <- NA
daily3.copy$diary.number <- 3; daily3.copy$consent <- NA; daily3.copy$Honesty <- NA
daily4.copy$diary.number <- 4; daily4.copy$consent <- NA; daily4.copy$Honesty <- NA
daily5.copy$diary.number <- 5; daily5.copy$consent <- NA; daily5.copy$Honesty <- NA
daily6.copy$diary.number <- 6; daily6.copy$consent <- NA; daily6.copy$Honesty <- NA
daily7.copy$diary.number <- 7; daily7.copy$consent <- NA; 
makeup1.copy$diary.number <- 8; makeup1.copy$consent <- NA; makeup1.copy$Honesty <- NA
makeup2.copy$diary.number <- 9; makeup2.copy$consent <- NA; makeup2.copy$Honesty <- NA
makeup3.copy$diary.number <- 10; makeup3.copy$consent <- NA; makeup3.copy$Honesty <- NA

merged.data.long <- rbind(daily1.copy,daily2.copy,daily3.copy,daily4.copy,daily5.copy,daily6.copy,daily7.copy,makeup1.copy,makeup2.copy,makeup3.copy)

# calculate average duration across 362 diaries that were done
table(merged.data.long$Duration..in.seconds.)
describe(merged.data.long$Duration..in.seconds.)

table(merged.data.long$Honesty)
describe(merged.data.long$Honesty)

merged.data.long$swls <- with(merged.data.long, (rowMeans(cbind(slws_4, slws_5, slws_6, slws_7, slws_8), na.rm=TRUE)))
merged.data.long$paffect <- with(merged.data.long, (rowMeans(cbind(panas_4, panas_6, panas_8, panas_19, panas_20, panas_22, panas_24, panas_26, panas_27, panas_29), na.rm=TRUE)))
merged.data.long$naffect <- with(merged.data.long, (rowMeans(cbind(panas_5, panas_7, panas_16, panas_17, panas_18, panas_21, panas_23, panas_25, panas_28, panas_30), na.rm=TRUE)))

with(merged.data.long, describe(cbind(swls,paffect,naffect)))
with(merged.data.long, corr.test(cbind(swls,paffect,naffect)))

with(merged.data.long, (alpha(data.frame(slws_4, slws_5, slws_6, slws_7, slws_8), na.rm=TRUE)))
with(merged.data.long, (alpha(data.frame(panas_4, panas_6, panas_8, panas_19, panas_20, panas_22, panas_24, panas_26, panas_27, panas_29), na.rm=TRUE)))
with(merged.data.long, (alpha(data.frame(panas_5, panas_7, panas_16, panas_17, panas_18, panas_21, panas_23, panas_25, panas_28, panas_30), na.rm=TRUE)))